<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div style="margin: 1rem 0 0 2.5rem; position: fixed; width: 100%">
      <el-row type="flex" justify="space-between">
        <el-col>
          <el-form :inline="true">
            <el-form-item label="关键字">
              <el-input
                type="text"
                width="100"
                placeholder="请输入作品关键词"
                v-model="searchObj.keyword"
                clearable
              />
            </el-form-item>

            <el-button type="primary" icon="el-icon-search" @click="fetchData()"
              >查询</el-button
            >
            <el-button type="default" @click="resetData">清空</el-button>
          </el-form>
        </el-col>
        <el-col>
          <el-button type="success" @click="add()">新增样片</el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 使用懒加载 -->
    <div
      class="infinite-list-wrapper"
      style="overflow: auto; margin-top: 260px"
    >
      <ul
        v-infinite-scroll="load"
        infinite-scroll-disabled="disabled"
        style="list-style: none"
      >
        <!-- <li v-for="i in count" class="list-item"> -->
        <li>
          <el-card :body-style="{ padding: '5px' }" shadow="always">
            <el-row type="flex" class="row-bg" justify="space-between">
              <el-col :span="18" style="display: flex">
                <div style="margin-right: 20px">
                  <img
                    src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"
                    class="custom-image"
                  />
                </div>
                <div
                  style="
                    display: flex;
                    flex-direction: column;
                    padding: 20px 15px;
                  "
                >
                  <div style="font-size: 20px">好吃的汉堡</div>
                  <div style="padding: 15px 0">
                    是否使用flex来控制垂直对齐布局,从技术上讲,没有必要。您可以使用自定义间距(填充和边距)在一列中垂直对齐您的内容/模块。例如,您可以使用
                    Divi 的间距选项为列提供相等的顶部和
                  </div>
                </div>
              </el-col>
              <el-col :span="6" class="button-card">
                <el-button type="text" icon="el-icon-edit">修改</el-button>
                <el-button
                  type="text"
                  @click="removeRows()"
                  style="margin: 0 20px"
                  icon="el-icon-delete"
                  >删除</el-button
                ></el-col
              >
            </el-row>
          </el-card>
        </li>
      </ul>
      <p v-if="loading">加载中...</p>
      <p v-if="noMore">没有更多了</p>
    </div>
  </div>
</template>

<script>
import carouselInfoApi from '@/api/show/carouselInfo'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      multipleSelection: [] // 批量选择中选择的记录列表
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },

    add() {
      this.$router.push({ path: '/product/skuInfo/add' })
    },

    // 加载banner列表数据
    fetchData(page = 1) {
      console.log('翻页。。。' + page)
      // 异步获取远程数据（ajax）
      this.page = page

      carouselInfoApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.records
          this.total = response.data.total

          // 数据加载并绑定成功
          this.listLoading = false
        })
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // promise
          // 点击确定，远程调用ajax
          return api.removeById(id)
        })
        .then((response) => {
          this.fetchData(this.page)
          if (response.code) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      console.log('handleSelectionChange......')
      console.log(selection)
      this.multipleSelection = selection
    },

    // 批量删除
    removeRows() {
      console.log('removeRows......')

      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录!'
        })
        return
      }

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // promise
          // 点击确定，远程调用ajax
          // 遍历selection，将id取出放入id列表
          var idList = []
          this.multipleSelection.forEach((item) => {
            idList.push(item.id)
            // console.log(idList)
          })
          // 调用api
          return api.removeRows(idList)
        })
        .then((response) => {
          this.fetchData(this.page)
          if (response.code) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    handlePublishStatusChange(index, row) {
      if (row.checkStatus !== 1) {
        this.$message({
          type: 'info',
          message: '审核通过才可以上级或下架'
        })
        row.publishStatus = row.publishStatus === 1 ? 0 : 1
        return
      }
      api.publish(row.id, row.publishStatus).then((response) => {
        this.$message({
          type: 'info',
          message: '操作成功'
        })
        this.fetchData()
      })
    },

    handleStatusChange(index, row) {
      api.check(row.id, row.checkStatus).then((response) => {
        this.$message({
          type: 'info',
          message: '操作成功'
        })
        this.fetchData()
      })
    },

    handleNewPersonChange(index, row) {
      api.isNewPerson(row.id, row.isNewPerson).then((response) => {
        this.$message({
          type: 'info',
          message: '操作成功'
        })
        this.fetchData()
      })
    }
  }
}
</script>
<style>
.button-card {
  display: flex;
  justify-content: center;
  align-items: center;
}
/* 根据需要设置图片的宽度和高度 */
.custom-image {
  width: 180px; /* 设置图片宽度为200像素 */
  height: auto; /* 保持高度与宽度的比例，避免图片拉伸 */
}
</style>
